package com.txw.javaCore.server.controller;

import com.google.common.collect.Maps;
import com.txw.javaCore.api.response.BaseResponse;
import com.txw.javaCore.api.response.StatusCode;
import com.txw.javaCore.model.request.PraiseRequest;
import com.txw.javaCore.server.service.PraiseService;
import com.txw.javaCore.server.util.ValidatorUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * @PROJECT_NAME: JavaCoreB2
 * @DESCRIPTION: 博客点赞业务
 * @DATE: 2021/3/18 13:32
 */
@RestController
@RequestMapping(value = "blog/praise")
public class PraiseController extends BaseController {

    @Autowired
    private PraiseService praiseService;

    /**
     * 点赞
     * @param request
     * @param result
     * @return
     */
    @PostMapping(value = "add", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public BaseResponse addPraise(@RequestBody @Validated PraiseRequest request, BindingResult result) {
        //检验参数的合法性
        String checkRes = ValidatorUtil.checkResult(result);
        if (StringUtils.isNotBlank(checkRes)) {
            return new BaseResponse(StatusCode.InvalidParams.getCode(), checkRes);
        }
        log.info("--前端接受的请求参数：{}", request);
        BaseResponse response = new BaseResponse(StatusCode.Success);
        HashMap<String, Object> resMap = Maps.newHashMap();
        try {
            log.info("--开始处理博客点赞业务--");
            //调用点赞博客处理方法，方式一：传统的处理方式
//            praiseService.addPraise(request);
            //调用点赞博客处理方法，方式二：加了分布式锁的处理方式
            praiseService.addPraiseLock(request);

            //获取博客的总点赞数
            Long total = praiseService.getBlogPraiseTotal(request.getBlogId());
            resMap.put("praiseTotal",total);
        } catch (Exception e) {
            log.error("博客点赞业务-点赞-发生异常：{}", e);
            return new BaseResponse(StatusCode.Fail.getCode(), e.getMessage());
        }
        response.setData(resMap);
        return response;
    }

    /**
     * 取消点赞
     * @param request
     * @param result
     * @return
     */
    @PostMapping(value = "cancel", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public BaseResponse cancelPraise(@RequestBody @Validated PraiseRequest request, BindingResult result) {
        //检验参数的合法性
        String checkRes = ValidatorUtil.checkResult(result);
        if (StringUtils.isNotBlank(checkRes)) {
            return new BaseResponse(StatusCode.InvalidParams.getCode(), checkRes);
        }
        log.info("--前端接受的请求参数：{}", request);
        BaseResponse response = new BaseResponse(StatusCode.Success);
        HashMap<String, Object> resMap = Maps.newHashMap();
        try {
            log.info("--开始处理博客取消点赞业务--");
            //调用取消点赞博客处理方法
            praiseService.cancelPraise(request);

            //获取博客的总点赞数
            Long total = praiseService.getBlogPraiseTotal(request.getBlogId());
            resMap.put("praiseTotal",total);
        } catch (Exception e) {
            log.error("博客点赞业务-取消点赞-发生异常：{}", e);
            return new BaseResponse(StatusCode.Fail.getCode(), e.getMessage());
        }
        response.setData(resMap);
        return response;
    }

    /**
     * 获取博客点赞排行榜
     * @return
     */
    @RequestMapping(value = "total/rank",method = RequestMethod.GET)
    public BaseResponse rankPraise(){
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            response.setData(praiseService.getRankWithRedisson());
        }catch (Exception e){
            log.error("获取博客点赞排行榜-发生异常：",e.fillInStackTrace());
            return new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }

}
